<?php  
	namespace Admin\Controller;
	class IntegralController extends CommonController
	{
		public function index()
		{
			/*$list1 = M('branch') -> select();
			$list2 = M('branch_and_creategory') -> select();
			$this -> assign('list1',$list1);
			$this -> assign('list2',$list2);
			*/
			$branch_id = I('param.branch_id');
			if(!empty($branch_id)){
				$where = "c.branch_id = $branch_id";
			}else{
				$where= "c.branch_id<>0";
				$branch_id = 0;
			}
			$list = M('category')
			->alias('c ')
			->join('__BRANCH__ b on b.id=c.branch_id','LEFT')
			->where($where) 
			->field('c.*,b.brank_name')
			-> select();
			$branches = M('branch')->select();
			$this->assign('branches',$branches);
			$this -> assign('list',$list);
			$this -> assign('branch_id',$branch_id);
			$this -> display();
		}

		/*public function AddBranch()
		{
			$branck_name = trim(I('post.branck_name'));
			if (!empty($branck_name)) 
			{
				$Model = new \Think\Model();
				$num = $Model->query("select count(*) as num from y_branch where brank_name=\"$branck_name\"");
				$sel = $num['0']['num'];
				if ($sel === '0') 
				{
					$data = [
							'brank_name' => $branck_name,
							'create_time' => time(),
							'update_time' => time(),
							'change_id' => $_SESSION['admin_info']['aid']
							];
					M('branch') -> data($data) -> add();
				}
			}
			$this -> redirect('Integral/index');
		}
		
		public function EditBranch()
		{
			$id = trim(I('post.id'));
			$brank_name = trim(I('post.branck_name'));
			if (!empty($brank_name)) 
			{
				$data = [
						'brank_name' => $brank_name,
						'update_time' => time(),
						'change_id' => $_SESSION['admin_info']['aid']
						];
				$where['id'] = $id;
				M('branch') -> where($where) -> save($data);
			}
			$this -> redirect('Integral/index');
		}

		public function DelBranch()
		{
			$id = trim(I('post.id'));
			$Model = new \Think\Model();
			$num = $Model->execute("delete from y_branch where id=\"$id\"");
			$this -> redirect('Integral/index');
		}
		*/
		public function AddCategory()
		{
			if(IS_POST){
				$branch_id = trim(I('branch_id'));
				$category_name = trim(I('category_name'));
				$min_jifen = trim(I('min_jifen'));
				$max_jifen = trim(I('max_jifen'));
				if($min_jifen>$max_jifen) return $this->error('积分区间错误');
				$number_add = trim(I('opt'));
				if (!empty($category_name) && $number_add!='') 
				{
					$num = M('category')->where("branch_id={$branch_id}")->where("category_name='{$category_name}'")->count();
					if ($num === '0') 
					{
						$data = [
								'branch_id' => $branch_id,
								'category_name' => $category_name,
								'min_jifen'=>$min_jifen,
								'max_jifen'=>$max_jifen,
								'opt' => $number_add,
								'create_time' => time(),
								'update_time' => time(),
								'change_id' => $_SESSION['admin_info']['aid']
								];
						M('category') -> data($data) -> add();
					}
				}

				$this -> redirect('Integral/index');
			}
			$branch_id = trim(I('param.branch_id'));
			$branches = M('branch')->select();
			$this->assign('branches',$branches);
			$this->assign('branch_id',$branch_id);
			$this->display();
			
		}

		public function EditCategory()
		{
			if(IS_POST){
				$category_id = trim(I('id'));
				$branch_id =  trim(I('post.branch_id'));
				$category_name = trim(I('post.category_name'));
				$opt = trim(I('post.opt'));
				$min_jifen = trim(I('post.min_jifen'));
				$max_jifen = trim(I('post.max_jifen'));
				if($min_jifen>$max_jifen){
					return $this->error('积分区间错误');
				}
				if (!empty($branch_id) && !empty($category_name) && $opt != '' || $opt == null ) 
				{
					$data = [
							'branch_id' => $branch_id,
							'category_name' => $category_name,
							'min_jifen'=>$min_jifen,
							'max_jifen'=>$max_jifen,
							'opt' => $opt,
							'update_time' => time(),
							'change_id' => $_SESSION['admin_info']['aid']
							];
					$where['id'] = $category_id;
					M('category') -> where($where) -> save($data);
				}
				$this -> redirect('Integral/index');
			}
			$category_id = trim(I('category_id'));
			$item = M('category')->where("id={$category_id}")->find();
			$branches = M('branch')->select();
			$this->assign('branches',$branches);
			$this->assign('item',$item);
			$this->display();
			
		}

		public function DelCategory()
		{
			$category_id = trim(I('category_id'));
			M('category')->where("id={$category_id}")->delete();
			$this->ajaxReturn(array('code'=>1,'msg'=>ok));
		}
	}